%%%----------------------------------------------------------------------
%%% File    :master_mnesia.erl
%%% Author  :
%%% Purpose : 
%%% Created : 13 Apr 2011 by
%%% Id      :
%%%----------------------------------------------------------------------

-module(master_mnesia).

-behaviour(gen_server).

-include("record.hrl").

%% External exports
-export([start/0,
	stop/0,
	register_nc/1]).

%% gen_server callbacks
-export([init/1,
	handle_call/3,
	handle_cast/2,
	code_change/3,
	handle_info/2,
	terminate/2]).

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
start() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
stop() ->
	gen_server:call(?MODULE, {stop}).
register_nc(NC_RESOURCE) ->
	gen_server:call(?MODULE, {register, NC_RESOURCE}).

%%%----------------------------------------------------------------------
%%% Callback functions from gen_server
%%%----------------------------------------------------------------------

%%----------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok, State}          |
%%          {ok, State, Timeout} |
%%          ignore               |
%%          {stop, Reason}
%%----------------------------------------------------------------------
init([]) ->
	database:create_database(),
	database:start(),
    {ok, ok}.

%%----------------------------------------------------------------------
%% Func: handle_call/3
%% Returns: {reply, Reply, State}          |
%%          {reply, Reply, State, Timeout} |
%%          {noreply, State}               |
%%          {noreply, State, Timeout}      |
%%          {stop, Reason, Reply, State}   | (terminate/2 is called)
%%          {stop, Reason, State}           (terminate/2 is called)
%%----------------------------------------------------------------------
handle_call({stop}, _From, State) ->
	Reply = ok,
	{stop, normal, Reply, State};
handle_call({register, NC_RESOURCE}, _From, State) ->
	database:insert(NC_RESOURCE),
	Reply = ok,
	{reply, Reply, State}.

%%----------------------------------------------------------------------
%% Func: handle_cast/2
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%%----------------------------------------------------------------------
handle_cast(_Msg, State) ->
    {noreply, State}.


code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%----------------------------------------------------------------------
%% Func: handle_info/2
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%%----------------------------------------------------------------------
handle_info(_Info, State) ->
    {noreply, State}.

%%----------------------------------------------------------------------
%% Func: terminate/2
%% Purpose: Shutdown the server
%% Returns: any (ignored by gen_server)
%%----------------------------------------------------------------------
terminate(_Reason, _State) ->
	database:stop(),
    ok.

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------

%%Internal functions
